package com.leanyuan.Infor.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.leanyuan.Infor.dao.AdminMapper;
import com.leanyuan.Infor.domain.Admin;
import com.leanyuan.Infor.domain.Role;
import com.leanyuan.Infor.domain.User;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class UserDetailsServiceImpl implements UserDetailsService {

    @Autowired
    private AdminMapper adminMapper;
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        if (username == null || "".equals(username)) {
            throw new RuntimeException("用户不能为空");
        };
        QueryWrapper<Admin> wrapper=new QueryWrapper();
        wrapper.eq("name",username);
        Admin admin=adminMapper.selectOne(wrapper);
        System.out.println(admin);
        if (admin == null) {
            throw new RuntimeException("用户不存在");
        };
        List<Role> arrayTypeList=new ArrayList<Role>(admin.getAdminId());
        return new User(admin.getAdminId().longValue(),admin.getUsername(),new BCryptPasswordEncoder().encode(admin.getPassword()),arrayTypeList);
    }
}
